<?php


namespace app\api\library\index\collageDetail;


use app\api\model\MallCollage;
use app\api\model\MallGoods;
use app\api\model\MallUser;
use think\facade\Db;

class V
{
    public static function run($param)
    {
        $userId    = _checkEmptyParam($param[ 'userId' ], '用户ID不能为空');
        $collageId = _checkEmptyParam($param[ 'collageId' ], '拼团ID不能为空');
        $collage   = Db::table('ea_mall_collage')
            ->field('collage_id,user_id,collage_no,goods_id,start_time,end_time,failure_reward,success_reward,collage_num+1 as collage_num,join_num+1 as join_num')
            ->where([['collage_id', '=', $collageId]])
            ->find();
        
        $collage[ 'success_reward' ] = getPrice($collage[ 'success_reward' ]);
        $collage[ 'failure_reward' ] = getPrice($collage[ 'failure_reward' ]);
        if (empty($collage)) {
            _outPut(201, [], '拼团不存在');
        }
        $goods = MallGoods::with([
            'spec' => function ($query) {
                $query->where('delete_time', '=', null);
            },
        ])
            ->where([['id', '=', $collage[ 'goods_id' ]]])
            ->field('id,title,logo,images,market_price,discount_price,novice_price,failure_reward,success_reward,virtual_sales,describe,collage_num+1 as collage_num')
            ->find();
        if (empty($goods)) {
            _outPut(201, [], '商品不存在或已下架');
        }
        $goods                     = $goods->toArray();
        $goods[ 'market_price' ]   = getPrice($goods[ 'market_price' ]);
        $goods[ 'discount_price' ] = getPrice($goods[ 'discount_price' ]);
        $goods[ 'novice_price' ]   = getPrice($goods[ 'novice_price' ]);
        $goods[ 'failure_reward' ] = getPrice($goods[ 'failure_reward' ]);
        $goods[ 'success_reward' ] = getPrice($goods[ 'success_reward' ]);
        $goods[ 'is_novice' ]      = MallUser::where('id', $userId)->value('is_novice');
//        if ($goods['is_novices_exclusive']==1&&$goods['is_novice']!=1&&$userId!=$collage['user_id']){
//            _outPut(201, [], '该商品只允许新手购买');
//        }
        $collage[ 'goods' ] = $goods;
        $users              = Db::name('mall_order')->where([['u_collage_id', '=', $collageId], ['order_status', 'in', '1,10,30,40,50,60']])->field('user_id,pay_time')->select();
        $users[]            = Db::name('mall_order')->where([['collage_id', '=', $collageId]])->field('user_id,pay_time')->find();
        $collage[ 'users' ] = [];
        if ( !empty($users)) {
            $users              = $users->toArray();
            $userIds            = array_column($users, 'user_id');
            $userIds[]          = $userId;
            $users              = array_column($users, null, 'user_id');
            $collage[ 'users' ] = MallUser::where([['id', 'in', $userIds]])->field('id,name,avatar_url')->select()->toArray();
            foreach ($collage[ 'users' ] as $key => &$value) {
                $value[ 'pay_time' ] = $users[ $value[ 'id' ] ][ 'pay_time' ] ?? '';
            }
        }
        if ( !empty($userIds)) {
            $users = Db::name('mall_user')->where([['id', 'in', $userIds]])->field('id,name,avatar_url')->select();
        }
        $collage[ 'invite_users' ] = $users;
        $collage[ 'my_collage' ]   = 0;
        if ($collage[ 'user_id' ] == $userId) {
            $collage[ 'my_collage' ] = 1;
        }
        $where              = [
            ['order_status', '>', 0],
            ['collage_id', '=', $collageId],
            ['user_id', '=', $userId],
            ['delete_time', '=', null],
        ];
        $collage[ 'order' ] = Db::name('mall_order')
            ->where($where)
            ->field('id,goods_id,order_no,order_price,order_status,collage_status,refund_status,collage_id,address_id,create_time')
            ->find();
        _outPut(200, $collage);
    }
}